package com.windliven.spoc.modules.salary.salarymanagement.service.impl;

import com.windliven.spoc.modules.salary.salarymanagement.dao.auto.UserMapper;
import com.windliven.spoc.modules.salary.salarymanagement.pojo.entity.User;
import com.windliven.spoc.modules.salary.salarymanagement.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author fly
 * @version V1.0
 * @date 2018/11/27
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @CacheEvict(cacheNames = {"users"}, allEntries = true)
    public void clearCache(){
        log.debug("clear Users cache.");
    }

    @Override
    @CachePut(cacheNames = "user", key = "#user.id")
    public User addUser(User user) {
        int i = userMapper.insertSelective(user);
        return userMapper.selectByPrimaryKey(user.getId());
    }


    @Override
    @CachePut(cacheNames = "user", key = "#user.id")
    public User updateUser(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        return userMapper.selectByPrimaryKey(user.getId());
    }

    @Override
    @CacheEvict(cacheNames = "user", key = "#user.id")
    public User delUser(User user) {
        int i = userMapper.deleteByPrimaryKey(user.getId());
        return new User();
    }

    @Override
    @Cacheable(cacheNames = "user", key = "#id")
    public User findUserById(Long id) {
        User user = new User();
        user.setId(id);
        User user1 = userMapper.selectByPrimaryKey(user);
        log.info("user:{}", user1);
        return user1;
    }

    @Override
    @Cacheable(cacheNames = "users")
    public List<User> findUsers() {
        return userMapper.selectAll();
    }
}
